var BaseModel = function () {
  var self = this;

  this.$alert = Vue.prototype.$alert;
  this.setDataFromObj = function (obj, fields, defaultValue, hasTrim) {
    for (var i = 0; i < fields.length; i++) {
      var field = fields[i];
      var value = obj[field];
      if (value === null || value === undefined) {
        if (defaultValue !== undefined) {
          value = defaultValue;
        }
      } else {
        if (hasTrim && typeof value === 'string') {
          value = value.trim();
        }
      }
      self[field] = value;
    }
  };

  this.initObject = function (fields, defaultValue) {
    if (!_.isArray(fields)) {
      fields = [fields];
    }

    if (defaultValue === undefined) {
      defaultValue = '';
    }

    for (var i = 0; i < fields.length; i++) {
      var field = fields[i];
      self[field] = defaultValue;
    }
  };

  this.setPostData = function (postData, propertyList, hasNullData, hasTrim) {
    if (propertyList === undefined) {
      propertyList = [];
    }

    if (hasNullData === undefined) {
      hasNullData = true;
    }

    for (var i = 0; i < propertyList.length; i++) {
      var property = propertyList[i];
      var value = _.get(this, property);
      if (value === undefined || value === '') {
        if (!hasNullData) {
          continue;
        }
        value = '';
      }

      if (typeof value === 'string') {
        if (hasTrim) {
          value = value.trim();
        }
      }
      _.set(postData, property, value);
    }
  };

  this.copyObject = function (postData, propertyList, hasNullData) {
    if (propertyList === undefined) {
      propertyList = [];
    }

    if (hasNullData === undefined) {
      hasNullData = true;
    }

    for (var i = 0; i < propertyList.length; i++) {
      var property = propertyList[i];
      var value = _.get(this, property);
      if (value === null || value === undefined) {
        if (!hasNullData) {
          continue;
        }
      }
      _.set(postData, property, value);
    }
  };

  this.setOldData = function (propertyList) {
    for (var i = 0; i < propertyList.length; i++) {
      var property = propertyList[i];
      var value = _.get(this, property);
      value = _.cloneDeep(value);
      _.set(this, `_old.${property}`, value);
    }
  };

  this.getOldData = function (property) {
    return _.get(this, `_old.${property}`);
  };

  this.getData = function (property, dataType) {
    var value = this[property];
    if (dataType === undefined) {
      return value;
    }

    if (value === null || value === undefined || value === '') {
      return value;
    }

    if (dataType === 'number') {
      if (typeof value === 'string') {
        value = value.trim();
      }
      if (value === '') {
        return value;
      }

      var count = Number(value);
      if (isNaN(count)) {
        return value;
      }
      return count;
    }
    return value;
  };

  this.isChange = function(propertyList){
    for (var i = 0; i < propertyList.length; i++) {
      var property = propertyList[i];
      var oldValue = this.getOldData(property);
      var value = this.getData(property);
      if (!_.isEqual(value,oldValue)){
        return true;
      }
    }
    return false;
  };

  this.getId = function () {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 32; i++) {
      s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[12] = "4";
    s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1);
    var uuid = s.join("");
    return uuid;
  };

  this.checkRequiredFields = function (fields, hasTrim) {
    for (var i = 0; i < fields.length; i++) {
      var field = fields[i];
      var value = this[field];
      if (hasTrim && typeof value === 'string') {
        value = value.trim();
      }
      if (SysUtil.isEmpty(value)) {
        return field;
      }
    }
    return '';
  };
};

var SearchBaseModel = function(){
  BaseModel.call(this);

  var self = this;
  var fn = {
    initData:function(){
      self.propertyList = [];
    }
  };

  this.saveOldData = function () {
    this.setOldData(this.propertyList);
  };

  this.resetOldData = function () {
    var propertyList = this.propertyList;
    for (var i = 0; i < propertyList.length; i++) {
      var property = propertyList[i];
      var value = this.getOldData(property);
      value = _.cloneDeep(value);
      this[property] = value;
    }
  };

  fn.initData();
};
